package org.techmore.activity;

import org.techmore.R;
import org.techmore.biz.CouponLoginDataHandler;
import org.techmore.zxing.client.android.CaptureActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class CouponLoginActivity extends Activity {
	
	private LinearLayout linearLayout = null;
	
	private SharedPreferences sharedPreferences = null;
	private SharedPreferences.Editor editor = null;
	
	private static String SAVE_NAME = "NAME_PREFERENCES";
	private static String SAVE_PWD = "PWD_PREFERENCES";
	
	private static String USER_NAME = "USER_NAME";
	private static String USER_PWD = "USER_PWD";
	
	private EditText acountID = null;
	private EditText acountPass = null;
	
	private CheckBox login_cb_savename = null;
	private CheckBox login_cb_savepwd = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setScreenOrientation();
		initView();
		
		acountID = (EditText) findViewById(R.id.acountID);
		acountPass = (EditText) findViewById(R.id.acountPass);
		login_cb_savename = (CheckBox) findViewById(R.id.login_cb_savename);
		login_cb_savepwd = (CheckBox) findViewById(R.id.login_cb_savepwd);
		
		initCheckBox();
		initEdite();
		
		Button loginButton = (Button) findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				CouponLoginDataHandler.getInstance().login(CouponLoginActivity.this, CouponLoginActivity.this, acountID.getText().toString(),  acountPass.getText().toString());
			}
		});
	}
	private void initCheckBox() {
		login_cb_savename.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				editor = sharedPreferences.edit();
				if(isChecked){
					editor.putBoolean(SAVE_NAME, true);
					String name = sharedPreferences.getString(USER_NAME, "");
					if(null != name && !"".equals(name)){
						editor.putString(USER_NAME, name);
					}else{
						editor.putString(USER_NAME, acountID.getText().toString());
					}
				}else{
					editor.putBoolean(SAVE_NAME, false);
				}
				editor.commit();
			}
		});
		login_cb_savepwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				editor = sharedPreferences.edit();
				if(isChecked){
					editor.putBoolean(SAVE_PWD, true);
					String pwd = sharedPreferences.getString(USER_PWD, "");
					if(null != pwd && !"".equals(pwd)){
						editor.putString(USER_PWD, pwd);
					}else{
						editor.putString(USER_PWD, acountPass.getText().toString());
					}
				}else{
					editor.putBoolean(SAVE_PWD, false);
				}
				editor.commit();
			}
		});
	}
	
	private void initEdite() {
		acountID.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				login_cb_savename.setChecked(false);
				return false;
			}
		});
		acountPass.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				login_cb_savepwd.setChecked(false);
				return false;
			}
		});
	}

	private void initView() {
		linearLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.techmore_login, null);
		RelativeLayout titleRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.titleRelativeLayout);
	    RelativeLayout footerRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.footerRelativeLayout);
	    
	    LinearLayout loginRelativeLayout = (LinearLayout) linearLayout.findViewById(R.id.loginLinearLayout);
	    LayoutParams contentLayoutParams = loginRelativeLayout.getLayoutParams();
		
		//獲取屏幕高度
        Integer contentScrollViewHeight = getWindowManager().getDefaultDisplay().getHeight();
        contentLayoutParams.height = contentScrollViewHeight - titleRelativeLayout.getLayoutParams().height - footerRelativeLayout.getLayoutParams().height-30 ;
        loginRelativeLayout.setLayoutParams(contentLayoutParams);
        setContentView(linearLayout);
        adapterFootButton(linearLayout);
	}
	
	
	 private void setScreenOrientation(){
	    	getWindow().requestFeature(Window.FEATURE_NO_TITLE);
	    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);		//豎屏
//	    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);		//橫屏
	    }
	 
	 
	
	 private void adapterFootButton(LinearLayout linearLayout){
	    	//獲取屏幕高度
	        DisplayMetrics displayMetrics = new DisplayMetrics();
	        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
	        Integer windowWidth = displayMetrics.widthPixels;
	        
	    	LinearLayout couponListButton = (LinearLayout) linearLayout.findViewById(R.id.couponListButton);
	        LinearLayout nearCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.nearCouponListButton);
	    	LinearLayout myCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.myCouponListButton);
	    	LinearLayout couponScanner = (LinearLayout) linearLayout.findViewById(R.id.couponScanner);
	    	
	    	RelativeLayout.LayoutParams layoutParams = null;
	    	
	    	if(240<windowWidth){
	    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponListButton.getLayoutParams();
	    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
	    		couponListButton.setLayoutParams(layoutParams);
	    		
	    		layoutParams = (android.widget.RelativeLayout.LayoutParams) nearCouponListButton.getLayoutParams();
	    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
	    		nearCouponListButton.setLayoutParams(layoutParams);
	    		
	    		layoutParams = (android.widget.RelativeLayout.LayoutParams) myCouponListButton.getLayoutParams();
	    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
	    		myCouponListButton.setLayoutParams(layoutParams);
	    		
	    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponScanner.getLayoutParams();
	    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
	    		couponScanner.setLayoutParams(layoutParams);
	    	}
	    	
	    	nearCouponListButton.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setClass(CouponLoginActivity.this, CouponMapActivity.class);
					CouponLoginActivity.this.startActivity(intent);
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//					CouponListActivity.this.finish();
					return false;
				}
			});
	    	couponListButton.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setClass(CouponLoginActivity.this, CouponListActivity.class);
					CouponLoginActivity.this.startActivity(intent);
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//					MyCouponListActivity.this.finish();
					return false;
				}
			});
	    	couponScanner.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CouponLoginActivity.this);
					if(preferences.getBoolean("isLogin",false)){
						Intent intent = new Intent();
						intent.setClass(CouponLoginActivity.this, CaptureActivity.class);
						CouponLoginActivity.this.startActivity(intent);
						overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
					}else{
						CouponLoginActivity.this.startActivity(new Intent(CouponLoginActivity.this,CouponLoginActivity.class));
					}
					return false;
				}
			});
	    	
	    	myCouponListButton.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setClass(CouponLoginActivity.this, MyCouponListActivity.class);
					CouponLoginActivity.this.startActivity(intent);
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//					CouponListActivity.this.finish();
					return false;
				}
			});
	    }
	 
	 @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);//this.getPreferences(MODE_PRIVATE);
		
		boolean saveName = sharedPreferences.getBoolean(SAVE_NAME, false);
		boolean savePwd = sharedPreferences.getBoolean(SAVE_PWD, false);
		
		if(saveName){
			acountID.setText(sharedPreferences.getString(USER_NAME, ""));
			login_cb_savename.setChecked(true);
		}else{
			login_cb_savename.setChecked(false);
		}
		if(savePwd){
			acountPass.setText(sharedPreferences.getString(USER_PWD, ""));
			login_cb_savepwd.setChecked(true);
		}else{
			login_cb_savepwd.setChecked(false);
		}
		
	}
	 
	



}
